//
//  HYQDatePickerHelper.m
//  HYQPickerView_Example
//
//  Created by zhuxingjian on 2018/8/28.
//  Copyright © 2018年 zxj1989. All rights reserved.
//

#import "HYQDatePickerHelper.h"

@interface HYQDatePickerHelper ()

@end

@implementation HYQDatePickerHelper

+ (NSArray *)yearArrayWithMin:(NSInteger)min max:(NSInteger)max
{
    NSMutableArray *years = [NSMutableArray arrayWithCapacity:0];
    for (NSInteger i = min; i < max; i++) {
        [years addObject:[NSString stringWithFormat:@"%zd年",i]];
    }
    return [years copy];
}

+ (NSArray *)monthArray;
{
    NSMutableArray *month = [NSMutableArray arrayWithCapacity:0];
    for (NSInteger i = 1; i <= 12; i++) {
        [month addObject:[NSString stringWithFormat:@"%zd月",i]];
    }
    return [month copy];
}

+ (NSArray *)daysArrayForYear:(NSInteger)year month:(NSInteger)month
{
    NSString *dateStr = [NSString stringWithFormat:@"%zd-%zd",year, month];
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM"];
    NSDate *date = [dateFormatter dateFromString:dateStr];
    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay
                                   inUnit: NSCalendarUnitMonth
                                  forDate:date];
    NSMutableArray *days = [NSMutableArray arrayWithCapacity:0];
    for (NSInteger i = 1; i <= range.length; i ++) {
        [days addObject:[NSString stringWithFormat:@"%zd日",i]];
    }
    return [days copy];
}

+ (NSArray *)hoursArray;
{
    NSMutableArray *hours = [NSMutableArray arrayWithCapacity:0];
    for (NSInteger i = 0; i < 24; i++) {
        NSString *str = nil;
        if (i < 10) {
            str = [NSString stringWithFormat:@"0%zd",i];
        }else {
            str = [NSString stringWithFormat:@"%zd",i];
        }
        [hours addObject:str];
    }
    return [hours copy];
}

+ (NSArray *)minutesArray;
{
    NSMutableArray *minutes = [NSMutableArray arrayWithCapacity:0];
    for (NSInteger i = 0; i < 60; i++) {
        NSString *str = nil;
        if (i < 10) {
            str = [NSString stringWithFormat:@"0%zd",i];
        }else {
            str = [NSString stringWithFormat:@"%zd",i];
        }
        [minutes addObject:str];
    }
    return [minutes copy];
}

+ (NSArray *)secondArray;
{
    NSMutableArray *seconds = [NSMutableArray arrayWithCapacity:0];
    for (NSInteger i = 0; i < 60; i++) {
        NSString *str = nil;
        if (i < 10) {
            str = [NSString stringWithFormat:@"0%zd",i];
        }else {
            str = [NSString stringWithFormat:@"%zd",i];
        }
        [seconds addObject:str];
    }
    return [seconds copy];
}

+ (NSDate *)formatDate:(NSString *)dateStr
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:dateStr];
    return date;
}

+ (NSString *)stringFromDate:(NSDate *)date;
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateStr = [dateFormatter stringFromDate:date];
    return dateStr;
}

+ (NSDictionary *)getDateInfoFromDate:(NSDate *)date;
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:date];
    
    NSInteger year = [components year];
    NSInteger month = [components month];
    NSInteger day = [components day];
    NSInteger hour = [components hour];
    NSInteger minute = [components minute];
    NSInteger second = [components second];
    
    return @{@"year" : @(year),
             @"month" : @(month),
             @"day" : @(day),
             @"hour" : @(hour),
             @"minute" : @(minute),
             @"second" : @(second)};
}

+ (NSDate *)dateWithYear:(NSInteger)year
                   month:(NSInteger)month
                     day:(NSInteger)day
                    hour:(NSInteger)hour
                  minute:(NSInteger)minute
                  second:(NSInteger)second;
{
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:day];
    [components setMonth:month];
    [components setYear:year];
    [components setHour:hour];
    [components setMinute:minute];
    [components setSecond:second];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *date = [calendar dateFromComponents:components];
    return date;
}



@end
